Imports System.Data
Imports System.Data.SqlClient

Public Class frmDataBase
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.IContainer

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
   Friend WithEvents Label3 As System.Windows.Forms.Label
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents editPassword As System.Windows.Forms.TextBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents editUsuario As System.Windows.Forms.TextBox
   Friend WithEvents Label4 As System.Windows.Forms.Label
   Friend WithEvents btnAceptar As System.Windows.Forms.Button
   Friend WithEvents btnCancelar As System.Windows.Forms.Button
   Friend WithEvents editBaseDeDatos As System.Windows.Forms.TextBox
   Friend WithEvents editServidor As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.GroupBox1 = New System.Windows.Forms.GroupBox
      Me.Label3 = New System.Windows.Forms.Label
      Me.editBaseDeDatos = New System.Windows.Forms.TextBox
      Me.Label2 = New System.Windows.Forms.Label
      Me.editPassword = New System.Windows.Forms.TextBox
      Me.Label1 = New System.Windows.Forms.Label
      Me.editUsuario = New System.Windows.Forms.TextBox
      Me.Label4 = New System.Windows.Forms.Label
      Me.editServidor = New System.Windows.Forms.TextBox
      Me.btnAceptar = New System.Windows.Forms.Button
      Me.btnCancelar = New System.Windows.Forms.Button
      Me.GroupBox1.SuspendLayout()
      Me.SuspendLayout()
      '
      'GroupBox1
      '
      Me.GroupBox1.Controls.Add(Me.Label3)
      Me.GroupBox1.Controls.Add(Me.editBaseDeDatos)
      Me.GroupBox1.Controls.Add(Me.Label2)
      Me.GroupBox1.Controls.Add(Me.editPassword)
      Me.GroupBox1.Controls.Add(Me.Label1)
      Me.GroupBox1.Controls.Add(Me.editUsuario)
      Me.GroupBox1.Controls.Add(Me.Label4)
      Me.GroupBox1.Controls.Add(Me.editServidor)
      Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
      Me.GroupBox1.Name = "GroupBox1"
      Me.GroupBox1.Size = New System.Drawing.Size(240, 128)
      Me.GroupBox1.TabIndex = 3
      Me.GroupBox1.TabStop = False
      '
      'Label3
      '
      Me.Label3.Location = New System.Drawing.Point(16, 48)
      Me.Label3.Name = "Label3"
      Me.Label3.Size = New System.Drawing.Size(88, 23)
      Me.Label3.TabIndex = 52
      Me.Label3.Text = "Base de Datos"
      '
      'editBaseDeDatos
      '
      Me.editBaseDeDatos.Location = New System.Drawing.Point(104, 48)
      Me.editBaseDeDatos.Name = "editBaseDeDatos"
      Me.editBaseDeDatos.Size = New System.Drawing.Size(120, 20)
      Me.editBaseDeDatos.TabIndex = 1
      Me.editBaseDeDatos.Text = ""
      '
      'Label2
      '
      Me.Label2.Location = New System.Drawing.Point(16, 96)
      Me.Label2.Name = "Label2"
      Me.Label2.Size = New System.Drawing.Size(88, 23)
      Me.Label2.TabIndex = 50
      Me.Label2.Text = "Password"
      '
      'editPassword
      '
      Me.editPassword.Location = New System.Drawing.Point(104, 96)
      Me.editPassword.Name = "editPassword"
      Me.editPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
      Me.editPassword.Size = New System.Drawing.Size(120, 20)
      Me.editPassword.TabIndex = 3
      Me.editPassword.Text = ""
      '
      'Label1
      '
      Me.Label1.Location = New System.Drawing.Point(16, 72)
      Me.Label1.Name = "Label1"
      Me.Label1.Size = New System.Drawing.Size(88, 23)
      Me.Label1.TabIndex = 48
      Me.Label1.Text = "Usuario"
      '
      'editUsuario
      '
      Me.editUsuario.Location = New System.Drawing.Point(104, 72)
      Me.editUsuario.Name = "editUsuario"
      Me.editUsuario.Size = New System.Drawing.Size(120, 20)
      Me.editUsuario.TabIndex = 2
      Me.editUsuario.Text = ""
      '
      'Label4
      '
      Me.Label4.Location = New System.Drawing.Point(16, 24)
      Me.Label4.Name = "Label4"
      Me.Label4.Size = New System.Drawing.Size(88, 23)
      Me.Label4.TabIndex = 45
      Me.Label4.Text = "Servidor"
      '
      'editServidor
      '
      Me.editServidor.Location = New System.Drawing.Point(104, 24)
      Me.editServidor.Name = "editServidor"
      Me.editServidor.Size = New System.Drawing.Size(120, 20)
      Me.editServidor.TabIndex = 0
      Me.editServidor.Text = ""
      '
      'btnAceptar
      '
      Me.btnAceptar.Location = New System.Drawing.Point(96, 144)
      Me.btnAceptar.Name = "btnAceptar"
      Me.btnAceptar.TabIndex = 4
      Me.btnAceptar.Text = "&Aceptar"
      '
      'btnCancelar
      '
      Me.btnCancelar.DialogResult = System.Windows.Forms.DialogResult.Cancel
      Me.btnCancelar.Location = New System.Drawing.Point(176, 144)
      Me.btnCancelar.Name = "btnCancelar"
      Me.btnCancelar.TabIndex = 5
      Me.btnCancelar.Text = "&Cancelar"
      '
      'frmDataBase
      '
      Me.AcceptButton = Me.btnAceptar
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.CancelButton = Me.btnCancelar
      Me.ClientSize = New System.Drawing.Size(256, 173)
      Me.Controls.Add(Me.GroupBox1)
      Me.Controls.Add(Me.btnAceptar)
      Me.Controls.Add(Me.btnCancelar)
      Me.Name = "frmDataBase"
      Me.ShowInTaskbar = False
      Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
      Me.Text = "Base de Datos"
      Me.GroupBox1.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private mstrServidor As String
   Private mstrBaseDeDatos As String

   Public Property Servidor() As String
      Get
         Servidor = mstrServidor
      End Get
      Set(ByVal Value As String)
         mstrServidor = Value
      End Set
   End Property

   Public Property BaseDeDatos() As String
      Get
         BaseDeDatos = mstrBaseDeDatos
      End Get
      Set(ByVal Value As String)
         mstrBaseDeDatos = Value
      End Set
   End Property

   Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
      Dim strServer As String
      Dim strDataBase As String
      Dim strUsuario As String
      Dim strPassword As String

      If editServidor.Text.Trim = "" Then
         MessageBox.Show("Debe escribir el Servidor", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)
         editServidor.Focus()
         Exit Sub
      End If

      If editBaseDeDatos.Text.Trim = "" Then
         MessageBox.Show("Debe escribir la Base de datos", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)
         editBaseDeDatos.Focus()
         Exit Sub
      End If

      If editUsuario.Text.Trim = "" Then
         MessageBox.Show("Debe escribir el Usuario", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Stop)
         editUsuario.Focus()
         Exit Sub
      End If

      strServer = editServidor.Text.Trim
      strDataBase = editBaseDeDatos.Text.Trim
      strUsuario = editUsuario.Text.Trim
      strPassword = editPassword.Text.Trim

      If VerificarBasedeDatos(strServer, strDataBase, strUsuario, strPassword) Then

         Dim strKey As Microsoft.Win32.RegistryKey
         strKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(gsRegPath)
         strKey.SetValue("Server", strServer)
         strKey.SetValue("DataBase", strDataBase)

         mstrServidor = strServer
         mstrBaseDeDatos = strDataBase

         Me.DialogResult = DialogResult.OK

      End If

   End Sub

   Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
      Me.DialogResult = DialogResult.Cancel
   End Sub

   Private Function VerificarBasedeDatos(ByVal strServer As String, ByVal strDataBase As String, ByVal strUsuario As String, ByVal strPassword As String) As Boolean
      Dim cnConn As New SqlConnection
      Dim strRetVal As String
      Dim blnRetVal As Boolean

      Try

            'cnConn.ConnectionString = "Data Source=" & strServer & ",1999;Network Library=DBMSSOCN;Initial Catalog=" & strDataBase & ";User ID=" & strUsuario & ";Password=" & strPassword
            cnConn.ConnectionString = "Data Source=" & strServer & ";Initial Catalog=" & strDataBase & ";User ID=" & strUsuario & ";Password=" & strPassword
         cnConn.Open()

         Try

            ' ----------------------------------------------------------------------------

            Dim cmdUsuarios As New SqlCommand
            cmdUsuarios.Connection = cnConn
            cmdUsuarios.CommandText = "Select Top 1 Usuario From pvUsuarios"

            strRetVal = cmdUsuarios.ExecuteScalar

            ' ----------------------------------------------------------------------------

            Dim cmdArticulos As New SqlCommand
            cmdArticulos.Connection = cnConn
            cmdArticulos.CommandText = "Select Top 1 InvtID From Inventory"

            strRetVal = cmdUsuarios.ExecuteScalar

            ' ----------------------------------------------------------------------------

            blnRetVal = True

         Catch ex As Exception

            MessageBox.Show("Base de Datos Incorrecta", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

         End Try

      Catch ex As SqlException

         MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception

            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally

            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If

        End Try

        Return blnRetVal

   End Function

   Private Sub frmDataBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      editServidor.Text = mstrServidor
      editBaseDeDatos.Text = mstrBaseDeDatos
      editUsuario.Text = "sa"

   End Sub

End Class
